package com.zbkj.client.controller;

import com.zbkj.client.config.ClientTokenComponent;
import com.zbkj.client.service.AgentClientService;
import com.zbkj.client.service.AgentCommonService;
import com.zbkj.client.service.ClientLoginService;
import com.zbkj.client.service.VideoServiceCommon;
import com.zbkj.common.model.client.agent.Agent;
import com.zbkj.common.model.client.agent.AgentGroup;
import com.zbkj.common.response.ClientLoginResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.AgentGroupService;
import com.zbkj.service.service.AgentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/client/agent")
@Api(tags = "代理管理")
public class AgentController {


    @Autowired
    private AgentCommonService agentCommonService;

    @Autowired
    private VideoServiceCommon videoServiceCommon;

    @Autowired
    private AgentService agentService;

    @Autowired
    private AgentGroupService agentGroupService;

    @Autowired
    private AgentClientService agentClientService;

    @Autowired
    private ClientLoginService clientLoginService;

    @Autowired
    private ClientTokenComponent clientTokenComponent;


    @ApiOperation(value = "获取代理列表")
    @GetMapping("/getList")
    public CommonResult<List<Agent>> getList() {
//        List<Map<String, Object>> agentList = agentCommonService.getAgentList();
        List<Agent> agentList = agentService.lambdaQuery()
                .eq(Agent::getIsDel, 0)
                .list();

        return CommonResult.success(agentList);
    }


    @ApiOperation(value = "获取代理详情")
    @GetMapping("/getAgentInfo")
    public CommonResult<Agent> getAgentInfo(@RequestParam String agentId) {
//        Map<String, Object> agentInfo = agentCommonService.getAgentInfo(agentId);

        Agent agent = agentService.lambdaQuery()
                .eq(Agent::getAgentId, agentId)
                .eq(Agent::getIsDel, 0)
                .one();

        return CommonResult.success(agent);
    }

    @ApiOperation(value = "获取群组列表")
    @GetMapping("/getGroupList")
    public CommonResult<List<Map<String, Object>>> getGroupList() {
        List<Map<String, Object>> groupList = agentCommonService.getGroupList();
        if (groupList != null && !groupList.isEmpty()) {
            return CommonResult.success(groupList);
        }

        return CommonResult.failed("获取群组列表失败");
    }


    @ApiOperation(value = "获取群组详情")
    @GetMapping("/getGroupInfo")
    public CommonResult<AgentGroup> getGroupInfo(@RequestParam String groupId) {
//        Map<String, Object> groupInfo = agentCommonService.getGroupInfo(groupId);
        AgentGroup agentGroup = agentGroupService.lambdaQuery()
                .eq(AgentGroup::getGroupId, groupId)
                .eq(AgentGroup::getIsDel, 0)
                .one();

        return CommonResult.success(agentGroup);
    }

    /**
     * 获取AI生成视频标题
     * @param params
     * @param httpRequest
     * @return
     */
    @PostMapping("/videoTitle")
    @ApiOperation(value = "获取AI生成视频标题")
    public CommonResult<?> videoTitle(@RequestBody Map<String, Object> params, HttpServletRequest httpRequest) {
        String content = (String) params.get("content");
        Map<String, Object> result = videoServiceCommon.videoTitle(content);

        Integer userId = clientTokenComponent.getLoginUser(httpRequest);
        String token = httpRequest.getHeader("token");
        ClientLoginResponse userInfo = clientTokenComponent.getUserInfo(userId, token);
        Integer merId = userInfo.getMerId();

        if (result != null && result.get("title") != null) {

//            clientLoginService.updateMerPoint((Integer) params.get("userId"),
//                    (Integer)params.get("merId"),
//                    "agent",
//                    userId,
//                    agentClientService.getMouleName("aiTitle"),
//                    "",
//                    agentClientService.getPointByDuration("aiTitle", null),
//                    null
//            );

            return CommonResult.success(result);
        }

        return CommonResult.failed((String) result.get("msg"));
    }
} 